package com.xuya.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xuya.dao.ProductDao;
import com.xuya.entity.Adminer;
import com.xuya.entity.Product;

public class SaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		Adminer adminer = (Adminer) session.getAttribute("user");
		if(adminer == null){
			resp.sendRedirect("index.jsp?code=10001");
			return;	
		}
		String name = req.getParameter("prodname");
		String price = req.getParameter("prodprice");
		if(name == null|"".equals(name)|price == null|"".equals(price)){
			resp.sendRedirect("add.jsp?code=10004");
			return;
		} 
		ProductDao pdao = new ProductDao();
		Product p = pdao.findProductByname(name);
		if(p != null){
			resp.sendRedirect("add.jsp?code=10005");
			//return;在这里不用写 ，如果else后面还有代码的话，就需要写
		}else{
			p = new Product();
			p.setName(name);
			p.setPrice(Float.valueOf(price));
			pdao.saveProduct(p);
			resp.sendRedirect("home.jspx");
		}
		
	}

}
